<template>
  <!-- 全局组件 -->
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container" @mouseleave="isShowGoods('hidden')">
      <h2 class="all" @mouseenter="isShowGoods('show')">全部商品分类</h2>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">晨锋超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      <!-- 添加动画 -->
      <transition name="myAnimation">
        <div class="sort" v-show="showGoodsList">
          <div class="all-sort-list2" @click="toSearch">
            <!-- 添加一个类  :class="{ item_on: true }" -->
            <div
              class="item bo"
              :class="{ item_on: showBackground === index }"
              @mouseenter="mouseOver(index)"
              @mouseleave="showBackground = -1"
              v-for="(list1, index) in categoryList"
              :key="list1.categoryId"
            >
              <!-- 使用编程时导航 跳转到search页面并带参 -->
              <h3>
                <!-- 添加自定义属性 将参数带进去 -->
                <a
                  href="javascript:;"
                  :data-category1Id="list1.categoryId"
                  :data-categoryName="list1.categoryName"
                >
                  {{ list1.categoryName }}
                </a>
              </h3>

              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    v-for="list2 in list1.categoryChild"
                    :key="list2.categoryId"
                    class="fore"
                  >
                    <dt>
                      <a
                        href="javascript:;"
                        :data-category2Id="list2.categoryId"
                        :data-categoryName="list2.categoryName"
                      >
                        {{ list2.categoryName }}
                      </a>
                    </dt>
                    <dd>
                      <em
                        v-for="list3 in list2.categoryChild"
                        :key="list3.categoryId"
                      >
                        <a
                          href="javascript:;"
                          :data-category3Id="list3.categoryId"
                          :data-categoryName="list3.categoryName"
                        >
                          {{ list3.categoryName }}
                        </a>
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
/* 引入 Lodash(处理防抖节流问题) */
import _ from "lodash";
/* 引入vuex辅助函数 */
/*  import { mapState } from "vuex"; */

export default {
  name: "TypeNav",
  data() {
    return {
      showBackground: -1 /* 控制鼠标移入移出高亮显示 */,
      showGoodsList: true /* 控制鼠标移入移出显示隐藏商品详情 */,
    };
  },
  mounted() {
    /*  */
    this.showGoodsList = this.$route.path === "/home";
  },
  computed: {
    /* 拿到vuex中的数据 */
    categoryList() {
      return this.$store.state.home.categoryList;
    },
    /* 通过辅助函数...mapState()拿到vuex中的数据 */
    // ...mapState({
    //   categoryList: (state) => state.home.categoryList,
    // }),
  },
  methods: {
    /* 鼠标移入节流处理 */
    mouseOver: _.throttle(function (index) {
      this.showBackground = index;
    }, 16),
    /* 跳转search页面并携带参数 */
    toSearch(event) {
      /* 处理IE浏览器兼容问题 */
      // event = event || window.event;
      if (event.target.nodeName === "A") {
        /* event.target.dataset 获取元素data-开头的属性的属性值(自定义属性) */
        // console.log(location);
        const { category1id, category2id, category3id, categoryname } =
          event.target.dataset;
        /* 配置跳转地址和参数 */
        const location = {
          name: "search",
          query: {
            // categoryId: category1id || category2id || category3id,
            categoryName: categoryname,
          },
          /* 如果this.$route.params中有数据就重新赋值给params 重新传过去 */
          params: this.$route.params,
        };
        if (category1id) {
          location.query.category1Id = category1id;
        } else if (category2id) {
          location.query.category2Id = category2id;
        } else {
          location.query.category3Id = category3id;
        }
        /* push跳转 对象形式*/
        this.$router.push(location);
      }
    },
    /* 控制鼠标移入移出 显示隐藏商品列表 */
    isShowGoods(flag) {
      /* 如果当前路由地址不是/home 就修改showGoodsList的默认值 */
      if (this.$route.path !== "/home") {
        /* flag等于show(鼠标移入事件) 就让showGoodsList=true(显示)，否则(鼠标移出事件)等于false(隐藏)  */
        this.showGoodsList = flag === "show";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      // 动画样式
      &.myAnimation-enter,
      &.myAnimation-leave-to {
        height: 0;
        opacity: 0;
      }
      &.myAnimation-enter-to {
        height: 461px;
        opacity: 1;
      }
      &.myAnimation-enter-active {
        transition: all 0.5s;
      }
      &.myAnimation-leave-active {
        transition: all 0.5s;
      }
      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &.item_on {
            background-color: #ccc;
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>